// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.parts;

import java.beans.PropertyChangeEvent;

import org.dengues.designer.ui.process.editors.GEFComponentsEditor;
import org.dengues.designer.ui.process.figures.CompConnectionTracFigure;
import org.dengues.designer.ui.process.models.CompConnectionTrac;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-8 下午10:11:07
 */
public class CompConnectionTracPart extends AbstractProcessPart {

    private CompConnectionTracFigure tracFigure;

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.AbstractProcessPart#getCastModel()
     */
    @Override
    protected CompConnectionTrac getCastModel() {
        return (CompConnectionTrac) getModel();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        tracFigure = new CompConnectionTracFigure(getCastModel(), true);
        return tracFigure;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
     */
    @Override
    protected void refreshVisuals() {
        Point loc = getCastModel().getLocation().getCopy();
        tracFigure.setCompConnectionTrac(getCastModel().getConnectionTrac());
        Dimension size = tracFigure.getPreferredSize();
        Point textOffset = new Point();
        textOffset.y = loc.y + GEFComponentsEditor.GRID_SIZE * 2;
        textOffset.x = loc.x - size.width / 2;

        Rectangle rectangle = new Rectangle(textOffset, size);
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), rectangle);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent evt) {
        refresh();
    }

}
